package com.cgs.query.find

import com.cgs.query.config.JpaCustomProperties
import com.cgs.query.config.JpaCustomPropertiesAware
import com.cgs.query.config.RunnerJdbcTemplateAware
import com.cgs.query.domain.ExecutionObj
import com.cgs.query.domain.QueryDefine
import com.cgs.query.handler.DefineFindHandler
import com.cgs.query.service.IExecutionObjService
import com.cgs.query.service.IQueryDefineService
import mu.KotlinLogging
import org.springframework.jdbc.core.JdbcTemplate

private val logger = KotlinLogging.logger { }

class QueryDefineFindHandler(
        private val queryDefineService: IQueryDefineService,
        private val executionObjService: IExecutionObjService
) : DefineFindHandler, RunnerJdbcTemplateAware, JpaCustomPropertiesAware {

    private lateinit var jdbcTemplate: JdbcTemplate

    private lateinit var jpaCustomProperties: JpaCustomProperties

    override fun findDefineById(defineId: String): QueryDefine? =
            queryDefineService.findById(defineId).apply { logger.debug { "查找查询定义:[$defineId]" } }

    override fun findExecutionObj(projectName: String, wholeName: String): ExecutionObj? {
        return executionObjService.findByProjectNameAndWholeName(projectName, wholeName)
    }

    override fun findExecutionObjById(id: String): ExecutionObj? {
        return executionObjService.findById(id)
    }

    override fun setRunnerJdbcTemplate(jdbcTemplate: JdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate
    }

    override fun setJpaCustomProperties(jpaCustomProperties: JpaCustomProperties) {
        this.jpaCustomProperties = jpaCustomProperties
    }

}